package game.editer.ui;

import hx.events.Event;
import hx.display.Quad;
import hx.display.InputLabel;

/**
 * 带标题的文本输入框组件
 */
class NumberInput extends BaseAttributeInput implements IAttributeInput {
	public var input:InputLabel;

	override function onInit() {
		super.onInit();

		// 输入功能
		input = new InputLabel();
		this.addChild(input);
		input.width = 120;
		input.height = 26;
		input.data = "0";
		input.textFormat = Config.COMMON_INPUT_TEXT_FORMAT;
		input.background = new Quad(115, 26, 0x3e3e3e);
		input.background.x = -2;
		input.background.y = 0;
		input.label.verticalAlign = MIDDLE;
		input.addEventListener(Event.CHANGE, (e) -> {
			this.onAttributeChange(this);
		});
	}

	override function set_value(value:Dynamic):Dynamic {
		input.data = Std.string(value);
		return super.set_value(value);
	}

	override function get_value():Dynamic {
		var value = Std.parseFloat(input.data);
		if (Math.isNaN(value)) {
			value = 0; // 如果解析失败，返回默认值0
		}
		return value;
	}
}
